package com.atguigu.gulimall.product.app;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;



/**
 * 属性分组
 *
 * @author lirl
 * @email 2608586849@qq.com
 * @date 2021-01-16 16:45:28
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 通过分类的3级catid获取属性组
     *  Map<String, Object> param,请求参数
     * {
     *    page: 1,//当前页码
     *    limit: 10,//每页记录数
     *    sidx: 'id',//排序字段
     *    order: 'asc/desc',//排序方式
     *    key: '华为'//检索关键字
     * }
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId ){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     * by attrGroupId 获取属性分组下的属性信息
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    //@RequiresPermissions("product:attrgroup:info")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId){
		List<AttrEntity> entites = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", entites);
    }

    /**
     * by attrGroupId 获取属性分组信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.findCateLogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }
    ///{attrgroupId}/noattr/relation
    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     * by attrgroupId
     */
    @RequestMapping("{attrgroupId}/noattr/relation")
    //@RequiresPermissions("product:attrgroup:info")
    public R  noRelationAttr(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrGroupId){
        PageUtils page = attrService.getNoRelationAttr(params,attrGroupId);
        return R.ok().put("data", page);
    }
    /**
     * 保存属性和分组的关联关系
     * 建立vo
     */
    @RequestMapping("/attr/relation")
    //@RequiresPermissions("product:attrgroup:save")
    public R saveAttrRelation(@RequestBody List<AttrGroupRelationVo> AttrGroupRelationVo){
       //调用别的服务的方法应该调用别的service
        attrAttrgroupRelationService.saveBatch(AttrGroupRelationVo);
        return R.ok();
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }
    /**
     * 删除属性分组的关联关系
     * 建立vo
     */
    @RequestMapping("attr/relation/delete")
    //@RequiresPermissions("product:attrgroup:save")
    public R deleteAttrRelation(@RequestBody List<AttrGroupRelationVo> AttrGroupRelationVo){
        //调用别的服务的方法应该调用别的service
        attrService.deleteAttrRelation(AttrGroupRelationVo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    /**
     * 新增商品时
     * 获取分类下的所有attrs和attrs groups
     */
    @RequestMapping("{catelogId}/withattr")
    //@RequiresPermissions("product:attrgroup:delete")
    public R getAttrGroipWhithAttrs(@PathVariable("catelogId") Long catelogId ){
        // 1.查询当前分类下的所有属性分组
        // 2.查询每个分组的所有信息
        List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrGroipWhithAttrsByCatelogId(catelogId);
        return R.ok().put("data", list);
    }

}
